package com.huawei.petSystemdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huawei.petSystemdemo.utils.MyRequest;
import com.huawei.petSystemdemo.utils.Request;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    TextView register;
    TextView username;
    TextView password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        register = findViewById(R.id.register2);
        username = findViewById(R.id.editTextTextPersonName1);
        password = findViewById(R.id.editTextTextPassword1);
        register.setOnClickListener(v->{
            if(ChickInput()){
                System.out.println("注册！！！");
                //MyRequest.baseUrl = "http://10.0.2.2:8080/front";//AS自带模拟器通用端口
                //MyRequest.baseUrl = "http://192.168.42.1:8080/front";//每次记得修改
                //MyRequest.baseUrl = Request.url;//在util/Request.java修改整体的地址


                Map<String,Object> requestBody = new HashMap<>();
                requestBody.put("username",username.getText().toString());
                requestBody.put("password",password.getText().toString());
                MyRequest.post("/user/register", requestBody, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Looper.prepare();//增加部分
                        Toast.makeText(RegisterActivity.this,"连接出错",Toast.LENGTH_SHORT).show();
                        System.out.println("注册失败");
                        Looper.prepare();//增加部分
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        String json = response.body().string();
                        System.out.println(json);
                        JSONObject jsonObject = JSON.parseObject(json);
                        int statusCode = jsonObject.getInteger("statusCode");
                        String errmsg = jsonObject.getString("errMessage");
                        if(statusCode == 400){
                            Looper.prepare();//增加部分
                            System.out.println(errmsg);
                            Toast.makeText(RegisterActivity.this,errmsg,Toast.LENGTH_SHORT).show();
                            Looper.loop();//增加部分
                        }
                        if(statusCode == 200){//注册成功

                            Looper.prepare();//增加部分
                            System.out.println(errmsg);
                            Toast.makeText(RegisterActivity.this,errmsg,Toast.LENGTH_SHORT).show();
                            finish();
                            Looper.loop();//增加部分
                        }



                    }
                });
            }

        });

    }

    public boolean ChickInput(){
        String StuNumber = username.getText().toString();
        String StuPwd = password.getText().toString();
        //trim() 方法用于删除字符串的头尾空白符
        if(StuNumber.trim().equals("")) {
            Toast.makeText(RegisterActivity.this,"用户名不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(StuPwd.trim().equals("")) {
            Toast.makeText(RegisterActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
//    public void onClick(View view){
//        Intent intent1 = new Intent(this,DelayMessageService.class);
//        intent1.putExtra(DelayMessageService.EXTRA_MESSAGE,getResources().getString(R.string.SuccessMessage));
//        startService(intent1);
//    }
}
